﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;

namespace BoarAccess
{
    public class LocalSession
    {
        public string SessionName, Offset, RepoPath, LocalPath;
        public int ID;
        internal static ResourceManager resources;

        private IEnumerable<SessionInfo> relevantSessions;

        public LocalSession(string filename, string searchPattern)
        {
            if (resources == null)
                resources = new ResourceManager();
            JObject jo = JObject.Parse(resources.RetrieveFileResource(filename));
            this.SessionName = (string)jo["session_name"];
            this.Offset = (string)jo["offset"];
            this.ID = (int)jo["session_id"];
            this.RepoPath = (string)jo["repo_path"];
            this.LocalPath = Directory.GetParent(Path.GetDirectoryName(filename)).ToString();
            //this.LocalPath = Directory.GetParent(filename).ToString();

            Console.WriteLine("Opened local session " + SessionName + " at " + RepoPath + " currently at revision " + ID);

            relevantSessions = loadRelevantSessions(latestRelevantSession());

            foreach (var info in relevantSessions)
            {
                Console.WriteLine(info);
            }

            foreach (var movName in Directory.GetFiles(LocalPath, searchPattern, SearchOption.AllDirectories))
            {
                var localName = movName.Replace(LocalPath + "\\", "");
                foreach (var fileInfo in this.RetrieveInfo(localName))
                {
                    Console.WriteLine(fileInfo);
                    Console.WriteLine("Remote modified: " + fileInfo.Modified + "; local modified: " + File.GetLastWriteTime(LocalPath + "\\" + fileInfo.Filename.Replace("/", "\\")));
                    Console.WriteLine("Remote created: " + fileInfo.Created + "; local created: " + File.GetCreationTime(LocalPath + "\\" + fileInfo.Filename.Replace("/", "\\")));

                    File.SetLastWriteTime(LocalPath + "\\" + fileInfo.Filename.Replace("/", "\\"), fileInfo.Modified);
                    File.SetCreationTime(LocalPath + "\\" + fileInfo.Filename.Replace("/", "\\"), fileInfo.Created);
                }
            }
        }

        private List<SessionInfo> loadRelevantSessions(SessionInfo latest)
        {
            List<SessionInfo> allInfos = new List<SessionInfo>();
            SessionInfo current = latest;
            while (current != null)
            {
                allInfos.Add(current);
                current = current.ParentSession;
            }
            return allInfos;
        }

        private SessionInfo latestRelevantSession()
        {
            //string[] sessionDirectories = Directory.GetDirectories(this.RepoPath + "\\sessions");
            for (int currentSession = this.ID; currentSession >= 0; currentSession-- )
            {

                string sessionDirectory = this.RepoPath + "\\sessions\\" + currentSession;
                string sessionInfoFilename = sessionDirectory + "\\session.json";
                SessionInfo session = new SessionInfo(this.ID, sessionInfoFilename);
                if (session.Name == this.SessionName)
                {
                    return session;
                }
            }

            return null;
        }

        public IEnumerable<FileInfo> RetrieveInfo(string filename)
        {
            Console.WriteLine("Searching for " + filename);
            var result = this.relevantSessions.Select(session => session.RetrieveFileInfo(filename)).Where(info => info != null);
            //result = result.Where(file => file.Filename == filename.Replace("\\", "/")).ToArray();
            return result;
        }
    }
}
